﻿Ext.namespace("Infrastructure.Tree");

Infrastructure.Tree = Ext.extend(Ext.tree.TreePanel, {
	initComponent: function () {
		this.hiddenPkgs = [];
		Ext.applyIf(this, {
			region: 'west',
			split: true,
			header: false,
			width: 280,
			minSize: 175,
			maxSize: 500,
			collapsible: true,
			margins: '0 0 5 5',
			cmargins: '0 0 0 0',
			rootVisible: false,
			lines: false,
			autoScroll: true,
			animCollapse: false,
			animate: false,
			collapseMode: 'mini',
			loader: new Ext.tree.TreeLoader({
				preloadChildren: true,
				clearOnLoad: false
			}),
			/*
			root: new Ext.tree.AsyncTreeNode({
			text: 'Ext JS',
			id: 'root',
			expanded: true,
			children: [Docs.classData]
			}),
			*/
			collapseFirst: false,
			tbar: [' ', new Ext.form.TextField({
				width: 150,
				emptyText: '请输入代码',
				enableKeyEvents: true,
				listeners: {
					render: function (f) {
						this.filter = new Ext.tree.TreeFilter(this, {
							clearBlank: true,
							autoClear: true
						});
					},
					keydown: {
						fn: this.filterTree,
						buffer: 350,
						scope: this
					},
					scope: this
				}
			}), ' ', ' ',
			{
				iconCls: 'icon-expand-all',
				tooltip: 'Expand All',
				handler: function () { this.root.expand(true); },
				scope: this
			}, '-', {
				iconCls: 'icon-collapse-all',
				tooltip: 'Collapse All',
				handler: function () { this.root.collapse(true); },
				scope: this
			}]
		});
		Infrastructure.Tree.superclass.initComponent.call(this);
		this.addEvents('clickNode');
	},
	filterTree: function (t, e) {
		var keyCode = e.keyCode;
		var text = t.getValue();
		Ext.each(this.hiddenPkgs, function (n) {
			n.ui.show();
		});
		if (!text) {
			this.filter.clear();
			return;
		}
		this.expandAll();

		//var re = new RegExp('^' + Ext.escapeRe(text), 'i');
		var re = new RegExp('.*' + Ext.escapeRe(text) + '.*', 'i');
		// javascript 转义，正则表达是再转义
		var regKey = new RegExp('^\\[' + Ext.escapeRe(text) + '\\].*', 'i');
		this.hiddenPkgs = [];
		var me = this;

		this.filter.filterBy(function (n) {
			// Press enter
			if (keyCode == '13') {
				if (regKey.test(n.text)) {
					me.fireEvent('clickNode', n);
				}
			}
			return !n.attributes.isExtPage || re.test(n.text);
			//return re.test(n.text);
		});

		// hide empty packages that weren't filtered

		this.root.cascade(function (n) {
			if (!n.attributes.isExtPage && n.ui.ctNode.offsetHeight < 3) {
				//if (n.ui.ctNode.offsetHeight < 3) {
				n.ui.hide();
				me.hiddenPkgs.push(n);
			}
		});
	},
	selectClass: function (cls) {
		if (cls) {
			var parts = cls.split('.');
			var last = parts.length - 1;
			var res = [];
			var pkg = [];
			for (var i = 0; i < last; i++) { // things get nasty - static classes can have .
				var p = parts[i];
				var fc = p.charAt(0);
				var staticCls = fc.toUpperCase() == fc;
				if (p == 'Ext' || !staticCls) {
					pkg.push(p);
					res[i] = 'pkg-' + pkg.join('.');
				} else if (staticCls) {
					--last;
					res.splice(i, 1);
				}
			}
			res[last] = cls;

			this.selectPath('/root/apidocs/' + res.join('/'));
		}
	},
	beforeselect: function (sm, node) {
		return node.isLeaf();
	}
});
